﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using HR60_PanelSoft.Tools;
using HR60_PanelSoft.View.Base;
using HR60_PanelSoft.View.TabForms;

namespace HR60_PanelSoft.View.Controls
{

    //GC: [g1]修正不保存到内存 [FormComboBox]
    //GC: [g2]内存对象不加载[FormComboBox]
    //GC: [g3]设备单次绑定限制[FormComboBox]
    //GC: [g4]bug2182/2183/2184[FormComboBox]
    //GC: [g5]优化：将核心的基础控件传递到外层[FormComboBox]
	public class FormComboBox : BaseFormControl
	{
		public static readonly object VALUE_NULL = new object();

		private ComboBox comboBox = new ComboBox();

		// 默认静态选项,常作标题用
		private SelectItem firstItem = null;

        /* g1.s1 将ComboBox当前选中项传递出去 */
        public SelectItem SelectedItem
        {
            get
            {
                if (null == comboBox.SelectedItem)
                    return null;
                SelectItem rst = new SelectItem(comboBox.Text,comboBox.SelectedValue);
                return rst;
            }
        }
        /* g1.s1 */

        /* [g4.s2]是否可以编辑内容 */

        public FormComboBox(bool CanEdit)
        {
            comboBox.Margin = new Padding(0);
            comboBox.Dock = DockStyle.Fill;
            comboBox.DropDownStyle = CanEdit? ComboBoxStyle.DropDown : ComboBoxStyle.DropDownList;
            /* [g4.s1]取消事件直接绑定 */
            //comboBox.SelectedValueChanged += new EventHandler(valueChange);
            comboBox.SelectedValueChanged += new EventHandler(valueChange_Normal);
            /* end of [g4.s1] */

            /* [g4.s6]解决编辑text不能立即刷新bug */
            if(CanEdit)
                comboBox.FormattingEnabled = true;
            /* end of [g4.s6] */
            this.AutoSize = false;
            this.Width = 121;
            this.Height = 20;
            this.Controls.Add(comboBox);
        }

		
        /* end of [g4.s2] */
		
		public FormComboBox()
		{
			comboBox.Margin = new Padding(0);
			comboBox.Dock = DockStyle.Fill;
			comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            /* [g4.s4]取消事件直接绑定 */
			//comboBox.SelectedValueChanged += new EventHandler(valueChange);
            comboBox.SelectedValueChanged += new EventHandler(valueChange_Normal);
            /* end of [g4.s4] */
			this.AutoSize = false;
			this.Width = 121;
			this.Height = 20;
			this.Controls.Add(comboBox);

		}

		public System.Windows.Forms.ComboBox.ObjectCollection Items
		{
			get
			{
				return comboBox.Items;
			}
		}

		/// <summary>
		/// 当前显示的文本
		/// </summary>
		public new string Text
		{
			set
			{
                //comboBox.SelectedText = value;
                comboBox.Text = value;
			}
			get
			{
				//return comboBox.SelectedText;
                return comboBox.Text;
			}
		}

		/// <summary>
		/// 设置静态选项
		/// </summary>
		/// <param name="item"></param>
		public void setStaticFirstItem(SelectItem item)
		{
			firstItem = item;
		}

		/// <summary>
		/// 设置静态选项
		/// </summary>
		/// <param name="text"></param>
		public void setStaticFirstItem(string text)
		{
			SelectItem item = new SelectItem(text, VALUE_NULL);
			firstItem = item;
		}

		/// <summary>
		/// 绑定数据
		/// </summary>
		/// <param name="data"></param>
		public void bindData(IDictionary data)
		{
			List<SelectItem> list = new List<SelectItem>();
			foreach (DictionaryEntry de in data)
			{
				list.Add(new SelectItem(de.Key.ToString(), de.Value));
			}
			bindItems(list);
		}

		/// <summary>
		/// 绑定数据
		/// </summary>
		/// <param name="data"></param>
		/// <param name="txtField"></param>
		/// <param name="valField"></param>
		public void bindData(IList data, string txtField, string valField)
		{
			List<SelectItem> list = new List<SelectItem>();
			foreach (object de in data)
			{
				IDictionary<string, object> ps = RefHelper.getFields(de);
				list.Add(new SelectItem(ps[txtField].ToString(), ps[valField]));
			}
			bindItems(list);
		}

		/// <summary>
		/// 绑定数据
		/// </summary>
		/// <param name="data"></param>
		/// <param name="txtField"></param>
		/// <param name="valField"></param>
		public void bindData(IList data, string txtField)
		{
			List<SelectItem> list = new List<SelectItem>();
			foreach (object de in data)
			{
				IDictionary<string, object> ps = RefHelper.getFields(de);
				list.Add(new SelectItem(ps[txtField].ToString(), de));
			}
			bindItems(list);
		}

		/// <summary>
		/// 绑定数据
		/// </summary>
		/// <param name="data"></param>
		public void bindData(object[] data)
		{
			List<SelectItem> list = new List<SelectItem>();
			foreach (object de in data)
			{
				list.Add(new SelectItem(de.ToString(), de));
			}
			bindItems(list);
		}

		/// <summary>
		/// 绑定选项列表
		/// </summary>
		/// <param name="list"></param>
		public void bindItems(List<SelectItem> list)
		{
			if (firstItem != null)
			{
				list.Insert(0, firstItem);
			}
			comboBox.DisplayMember = "text";
			comboBox.ValueMember = "value";
			comboBox.DataSource = list;
		}
		
		/// <summary>
		/// 清除选择项
		/// </summary>
		public void cleanItems()
		{
			comboBox.DataSource = null;
			comboBox.Items.Clear();
		}

        /*[g2.s4] */
        public int findAt(object value)
        {
            int rst = -1,i = 0;
            SelectItem si = new SelectItem(string.Empty, value);
            foreach (SelectItem x in comboBox.Items)
            {
                if (x.value == value)
                {
                    rst = i;
                    break;
                }
            }
            return rst;
        }
        /*  */

		// 重写Value属性
		public override object Value
		{
			get
			{
				return this.comboBox.SelectedValue == VALUE_NULL ? null : this.comboBox.SelectedValue;
			}
			set
            {
                /* [g2.s4] 处理面板编辑类型不变bug*/
                //this.comboBox.SelectedValue = value == null ? VALUE_NULL : value;
                if (null == value)
                    this.comboBox.SelectedValue = VALUE_NULL;
                //else if (this.comboBox.Items.Contains())
                //    this.comboBox.SelectedValue = value;
                else
                //this.comboBox.SelectedValue = value;
                {
                    this.comboBox.SelectedValue = value;
                    /* test */
                    //int tempN = 0;
                    //List<SelectItem> tempList = new List<SelectItem>();
                    //tempList.Clear();
                    //foreach (object o in comboBox.Items)
                    //{
                    //    tempList.Add((SelectItem)o);
                    //}
                    //tempN = tempList.FindIndex(new Predicate<SelectItem>((x)=>
                    //    {
                    //        return x.text == value.ToString();
                    //    }));
                    /* end of test */

                }

                /* end of [g2.s4] */
            }
		}

        //GC: 订制事件
        public void addDropDownEvent(EventHandler eh)
        {
            comboBox.DropDown += eh;
            
        }

        /* [g3.s1]增加DropDownClosed事件传递 */
        public void addDropDownClosedEvent(EventHandler eh)
        {
            comboBox.DropDownClosed += eh;
        }

        /* end of [g3.s1] */

        /* [g4.s5]增加TextChanged事件传递 */

        public void addTextChangedEvent(EventHandler eh)
        {
            comboBox.TextChanged += eh;
        }

        /* end of [g4.s5] */

        /* [g5.s1] */
        public override List<Control> GetCoreControls()
        {
            List<Control> rst = new List<Control>();
            rst.Clear();
            rst.Add(comboBox);
            return rst;
        }

        /* end of [g5.s1] */











	}
}
